package com.qf.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.annotation.Log;
import com.qf.entity.YuYueOrderDTO;
import com.qf.entity.YuYueOrder;
import com.qf.listener.YuYueOrderExcelListener;
import com.qf.service.YuYueOrderService;
import com.qf.util.R;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * --- 冲 ---
 *
 * @author Biggen
 * @date 2025/4/27
 * @desc
 */
@RestController
@CrossOrigin
@RequestMapping("/zf")
public class YuYueOrderPageController {
    private Logger logger = LogManager.getLogger(YuYueOrderPageController.class);


    @Autowired
    private YuYueOrderService service;

    @GetMapping("/order/queryAllPage")
    @Log(value = "查询所有预约订单",type = "查询")
    public R queryAllOrderPage(YuYueOrderDTO yuYueOrderDTO){
        PageHelper.startPage(yuYueOrderDTO.getPageNum(),yuYueOrderDTO.getPageSize());
        List<YuYueOrder> list = service.queryAllOrderPage(yuYueOrderDTO);
        PageInfo<YuYueOrder> pageInfo = new PageInfo<>(list);
        return R.ok(pageInfo);
    }

    /**
     * 上传Excel文件
     */
    @PostMapping("/upload/yuYueOrderExcel")
    @Log(value = "上传Excel文件",type = "上传")
    public R uploadYuYueOrderExcel(MultipartFile file){
        try {
            EasyExcel.read(file.getInputStream(), YuYueOrder.class, new YuYueOrderExcelListener(service)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }


    /**
     * 导出Excel文件
     */
    @GetMapping("/download/yuYueOrder")
    public void exportExcel(HttpServletResponse response) throws IOException {
        // 查询所有预约订单
        List<YuYueOrder> list = service.queryAllOrderPage(null);
        // 设置内容类型为Excel
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        // 设置编码格式
        response.setCharacterEncoding("utf-8");
        // URLEncoder.encode()方法对中文进行编码
        String fileName = URLEncoder.encode("预约订单😀.xlsx", "UTF-8").replaceAll("\\+", "%20");
        // 设置Content-Disposition响应头，用于告诉浏览器以何种方式打开响应体
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        // 导出Excel
        EasyExcel.write(response.getOutputStream(), YuYueOrder.class).sheet("预约订单").doWrite(list);
    }
}
















